    <template>
        <section>

            <!--工具条-->
            <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
                <el-form :inline="true">
                    <el-form-item style="float: right">
                        <el-tag>试卷管理</el-tag>
                    </el-form-item>
                    <el-form-item style="float: left">
                        <el-button @click="reload" size="mini" round>返回</el-button>
                    </el-form-item>
                    <el-form-item style="float: right">
                        <el-button type="primary" @click="showAdd" icon="el-icon-plus">创建试卷</el-button>
                    </el-form-item>
                </el-form>
            </el-col>

            <!--试卷列表-->
            <el-table
                    :data="tableData"
                    stripe border highlight-current-row
                    style="width: 100%"
                    v-loading="loading"
            >
                <el-table-column type="index" width="60">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="试卷名"
                        min-width="300px">
                </el-table-column>
                <el-table-column
                        prop="dlevel"
                        label="难度类型"
                        min-width="100px">
                </el-table-column>
                <el-table-column
                        prop="operation"
                        label="操作"
                        min-width="200px">
                    <template slot-scope="scope" >
                        <el-button size="mini" @click="showExamPaper(scope.row.id)" icon="el-icon-tickets" >查看详情</el-button>
                       <!-- <el-button size="mini" @click="" icon="el-icon-edit" >编辑</el-button>-->
                        <el-button size="mini" @click="Gocomposepaper(scope.row.id,scope.row.name,scope.row.dlevel,chuangzhi)" icon="el-icon-edit" >组卷</el-button>
                        <el-button :v-show="iscreateteacher" size="mini" @click="showshare(scope.row.id)" icon="el-icon-share">共享</el-button>
                        <el-button size="mini" @click="" icon="el-icon-delete" >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!--创建试卷-->
            <el-dialog :title="formTitle" :visible.sync="claimVisible" :close-on-click-modal="false">
                <el-form :rules="rules" label-position="left" label-width="80px">
                    <el-form-item label="试卷名" prop="name">
                        <el-input v-model="formdata.name" placeholder="请输入试卷名"></el-input>
                    </el-form-item>
                    <el-form-item label="总分" prop="totalpoints">
                        <el-input v-model="formdata.totalpoints" placeholder="请输入总分"></el-input>
                    </el-form-item>
                    <el-form-item label="难度" size="mini">
                        <el-radio-group v-model="formdata.dlevel" prop="dlevel">
                            <el-radio label="1" border>简单</el-radio>
                            <el-radio label="2" border>中等</el-radio>
                            <el-radio label="3" border>困难</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="题型" size="mini">
                        <el-select v-model="value4" multiple placeholder="请选择">
                            <el-option v-for="item in questiontype" :label="item.label" :value="item.value">
                            </el-option>
                        </el-select>
                       <!-- <el-input-number v-model="formdata.noet" :min="1"></el-input-number>-->
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click.native="claimVisible = false">取消</el-button>
                    <el-button type="primary" v-on:click="add" :loading="submitLoading">提交</el-button>
                </div>
            </el-dialog>

            <!--试卷详情-->
            <el-dialog @close="typeshow" :title="examname" :visible.sync="exampaperinfoVisible" :close-on-click-modal="false">
                <div>
                    <div :v-model="exampaperinfo" style="text-align: center">{{exampaperinfo.name}}</div>
                </div>
                <div :data="paperExerciseList">
                    <ul style="list-style: none;padding: 0;margin: 0">
                        <span v-show="isshow1">一、单选题：</span>
                        <li>
                            <ol>
                                <li v-if="item.exerciseInfo.type=='1'" v-for="item in  paperExerciseList ">
                                    <span></span> {{ item.choice.content}}
                                    <div> <span>A</span> {{item.choice.labela}}</div>
                                    <div> <span>B</span> {{item.choice.labelb}}</div>
                                    <div> <span>C</span> {{item.choice.labelc}}</div>
                                    <div> <span>D</span> {{item.choice.labeld}}</div>
                                </li>
                            </ol>
                        </li>
                        <span v-show="isshow2">二、多选题：</span>
                        <li>
                            <ol>
                                <li v-if="item.exerciseInfo.type=='2'" v-for="item in  paperExerciseList ">
                                    <span></span> {{ item.choice.content}}
                                    <div> <span>A</span> {{item.choice.labela}}</div>
                                    <div> <span>B</span> {{item.choice.labelb}}</div>
                                    <div> <span>C</span> {{item.choice.labelc}}</div>
                                    <div> <span>D</span> {{item.choice.labeld}}</div>
                                    <div v-if="item.choice.labele ==null"> <span>E</span> {{item.choice.labele}}</div>
                                    <div v-if="item.choice.labelf ==null"> <span>F</span> {{item.choice.labelf}}</div>
                                </li>
                            </ol>
                        </li>
                        <span v-show="isshow3">三、判断题：</span>
                        <li>
                            <ol>
                                <li v-if="item.exerciseInfo.type=='3'" v-for="item in  paperExerciseList ">
                                    <span></span> {{ item.trueFalse.content}}
                                </li>
                            </ol>
                        </li>
                        <span v-show="isshow4">四、填空题：</span>
                        <li>
                            <ol>
                                <li v-if="item.exerciseInfo.type=='4'" v-for="item in  paperExerciseList ">
                                    <span></span>{{ item.completion.content}}
                                </li>
                            </ol>
                        </li>
                        <span v-show="isshow5">五、简答题：</span>
                        <li>
                            <ol>
                                <li v-if="item.exerciseInfo.type=='5'" v-for="item in  paperExerciseList ">
                                    <span></span>{{ item.shortAnswer.content}}
                                </li>
                            </ol>
                        </li>
                        <span v-show="isshow6">六、程序设计题：</span>
                        <li>
                            <ol>
                                <li v-if="item.exerciseInfo.type=='6'" v-for="item in  paperExerciseList ">
                                    <span></span>{{ item.programming.content}}
                                </li>
                            </ol>
                        </li>
                    </ul>
                </div>
            </el-dialog>

            <!--共享试卷界面-->
            <el-dialog title="共享试卷" :visible.sync="exampapershareVisible" :close-on-click-modal="false">
                <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
                <el-checkbox-group v-model="checkedTeachers" @change="handleCheckedTeachersChange">
                    <el-checkbox v-if="item.teacherInfo.id!=teacherid" v-for="item in teacherOptions" :label="item.teacherInfo.id" >
                        {{item.user.realname}}</el-checkbox>
                </el-checkbox-group>
            </el-dialog>
        </section>
    </template>

    <script>
        export default {
            data() {
                return {
                    loading:true,
                    value4:[],
                    questiontype:[
                        {
                            label:'单选题',
                            value:'1'
                        },{
                            label:'多选题',
                            value:'2'
                        },{ label:'判断题',
                            value:'3'},
                        { label:'填空题',
                            value:'4'},
                        { label:'简答题',
                            value:'5'},
                        { label:'程序设计题',
                            value:'6'}
                    ],
                    tableData:[
                    ],
                    teacherOptions:[],
                    checkedTeachers:[],
                    formTitle:"创建试卷",
                    exampaperinfoVisible:false,
                    submitLoading: false,
                    examname:'',
                    formdata:{
                        id:'',
                        name:'',
                        noe:'',
                        totalpoints:'',
                        dlevel:'',
                        noet:'',
                        courseid:'',
                        teacherid:'',
                        isshare:'2'
                    },
                    exampaperinfo:{
                        id:'',
                        name:'java',
                        noe:'',
                        totalpoints:'',
                        dlevel:'',
                        noet:'',
                        courseid:'',
                        teacherid:'',
                        isshare:''
                    },
                    name:'',
                    total:'20',
                    teacherid:'',
                    courseid:'',
                    user:{},
                    claimVisible:false,
                    rules:{
                        name: [
                            {required: true, message: '请输入试卷名', trigger: 'blur'}
                        ],
                        totalpoints:[
                            {required: true, message: '请输入总分', trigger: 'blur'}
                        ],
                        dlevel:[
                            { required: true, message: '请选择难度类型', trigger: 'change' }
                        ]
                    },
                    paperExerciseList:[],
                    isshow1:false,
                    isshow2:false,
                    isshow3:false,
                    isshow4:false,
                    isshow5:false,
                    isshow6:false,
                    iscreateteacher:false,
                    exampapershareVisible:false,
                    isIndeterminate:true,
                    checkAll:false,

                    chuangzhi:'',
                };
            },
            methods:{
                Gocomposepaper(num,label,de,cz){
                    this.$router.push({path:"/composepaper",query:{id:num,name:label,dlevel:de,chuangzhi:cz}});
                    console.log(label);
                },
                showExamPaper(num){
                    this.exampaperinfoVisible = true;
                    this.examname='试卷详情';
                    this.getExampaperInfo(num);

                },
                showshare(num){
                    this.exampapershareVisible=true;
                    this.getTeacherOptions();
                },
                showAdd: function () { //显示单个新增界面
                    this.claimVisible = true;
                    this.formTitle = '创建试卷';
                },
                reload(){
                    this.$router.push({path:"/courseManage"});
                },

                //打印每门课程下当前教师能看得见的试卷
                getTableData(){
                    this.$http.post('/api/exampaper/findBycondition/'+this.teacherid+"/"+this.courseid).then(res=>{
                        for(let i=0;i<res.data.data.length;i++){
                            if(res.data.data[i].dlevel===1){
                                res.data.data[i].dlevel="简单";
                            }
                            else if(res.data.data[i].dlevel===2){
                                res.data.data[i].dlevel="中等";
                            }
                            else if(res.data.data[i].dlevel===3){
                                res.data.data[i].dlevel="困难";
                            }
                        }
                        this.tableData=res.data.data;
                        for(let i=0;i<this.tableData.length;i++){
                            this.isshare(this.tableData[i].id);

                        }
                        console.log("1111111111111");
                        console.log(this.tableData);
                    }).catch(error=>{

                    });
                },

                //获得教师信息
                getTeacherInfo(){
                    this.user = JSON.parse(localStorage.getItem("CurrentUser"));//获取userid
                    this.$http.get('/api/teacherinfo/findTeacherByUserid/'+this.user.id).then(res=>{
                        this.teacherid=res.data.teacherInfo.id;
                        console.log(this.teacherid);
                        this.getTableData();
                        this.loading=false;
                    }).catch((error)=>{

                    });
                },

                //创建试卷
                add(){
                    this.courseid = this.$route.query.courseid;
                    this.user = JSON.parse(localStorage.getItem("CurrentUser"));//获取userid
                    this.$http.get('/api/teacherinfo/findTeacherByUserid/'+this.user.id).then(res=>{
                        this.teacherid=res.data.teacherInfo.id;
                        console.log(this.teacherid);
                        this.getTableData();
                    }).catch((error)=>{

                    });
                    this.formdata.courseid=this.courseid;
                    this.formdata.teacherid=this.teacherid;
                    this.submitLoading=true;
                    this.$http.post('api/exampaper/add',this.formdata).then(res=>{
                        if(!res.data.code){
                            this.$message({
                                message: '创建试卷成功',
                                type: 'success',
                            });
                            this.getTableData();
                            this.claimVisible=false;
                            this.formdata.dlevel='';
                            this.formdata.totalpoints='';
                            this.formdata.noe='';
                            this.formdata.noet='';
                            this.formdata.name='';
                            this.formdata.courseid='';
                            this.formdata.teacherid='';
                            this.formdata.isshare='2';
                        }else{
                            this.$message({
                                message: '创建试卷失败',
                                type: 'error'
                            });
                        }
                        this.submitLoading=false;
                    }).catch(error=>{
                        console.log(error);
                    });
                },

                //获取试卷信息
                getExampaperInfo(num){
                    this.$http.post('/api/exampaper/findByid/'+num).then(res=>{
                        this.exampaperinfo=res.data.data;
                        console.log(this.exampaperinfo);
                    }).catch((error)=>{
                        console.log(error);
                    });
                    this.$http.post("/api/composePaper/findByPaperid/"+num).then((res)=>{
                        console.log(res.data);
                        this.paperExerciseList=res.data.data;
                        for(let i=0;i<this.paperExerciseList.length;i++){

                            if(this.paperExerciseList[i].exerciseInfo.type===1){

                                this.isshow1=true;
                            }
                            else if(this.paperExerciseList[i].exerciseInfo.type===2){

                                this.isshow2=true;
                            }
                            else if(this.paperExerciseList[i].exerciseInfo.type===3){

                                this.isshow3=true;
                            }
                            else if(this.paperExerciseList[i].exerciseInfo.type===4){

                                this.isshow4=true;
                            }
                            else if(this.paperExerciseList[i].exerciseInfo.type===5){

                                this.isshow5=true;
                            }
                            else if(this.paperExerciseList[i].exerciseInfo.type===6){

                                this.isshow6=true;
                            }

                        }
                        this.questiontype=this.tmp;
                        console.log('这是题型'+this.questiontype);
                    }).catch((error)=>{
                        console.log(error);
                    });
                },

                //重置题型显示
                typeshow(){
                    this.isshow1 = false;
                    this.isshow2 = false;
                    this.isshow3 = false;
                    this.isshow4 = false;
                    this.isshow5 = false;
                    this.isshow6 = false;
                },

                //判断试卷是否登陆教师所创建，他是否有共享的权利
                isshare(num){
                    this.user = JSON.parse(localStorage.getItem("CurrentUser"));//获取userid
                    this.$http.get('/api/teacherinfo/findTeacherByUserid/'+this.user.id).then(res=>{
                        this.teacherid=res.data.teacherInfo.id;
                    }).catch((error)=>{
                        console.log(error);
                    });
                    this.$http.post('/api/exampaper/findByid/'+num).then(res=>{
                        this.exampaperinfo=res.data.data;
                    }).catch((error)=>{
                        console.log(error);
                    })
                    if(this.teacherid==this.exampaperinfo.teacherid){
                        this.iscreateteacher=true;
                    }
                },

                handleCheckAllChange(val){
                    if(val) {
                        for (let i = 0; i < this.teacherOptions.length; i++) {
                            if (this.teacherid != this.teacherOptions[i].teacherInfo.id) {
                                this.checkedTeachers.unshift(this.teacherOptions[i].teacherInfo.id);
                            }
                        }
                    }
                    else {
                        this.checkedTeachers=null;
                    }
                    console.log(this.checkedTeachers);
                    this.isIndeterminate = false;
                },
                handleCheckedTeachersChange(value){
                    let checkedCount = value.length;
                    console.log(value);
                    this.checkAll = checkedCount === (this.teacherOptions.length-1);
                    this.isIndeterminate = checkedCount > 0 && checkedCount < (this.teacherOptions.length-1);
                },
                getTeacherOptions(){
                    this.$http.get('/api/courseteach/findTeacherListByCourseid/'+this.courseid).then(res=>{
                        this.teacherOptions=res.data.teacherList;
                        console.log(this.teacherOptions);
                    }).catch((error)=>{
                        console.log(error);
                    });
                }
            },
            mounted(){
                //进入页面就加载登陆账户信息相关的课程ID和教师信息
                this.courseid = this.$route.query.courseid;
                this.chuangzhi=this.courseid;
                this.getTeacherInfo();
            }
        }

    </script>

    <style scoped>

    </style>